Page({
  data: {
    currentTab: 0,
    scrollLeft: 0, // 导航滚动位置
    tabs: ["精选", "同城", "游戏", "明星", "聊天", "唱歌"],
    funcList: [
      { name: '经验', icon: '/static/icon-images/经验值.png' },
      { name: '热点', icon: '/static/icon-images/热点.png' },
      { name: '直播', icon: '/static/icon-images/直播.png' },
      { name: '团购', icon: '/static/icon-images/团购.png' },
    ],
    liveList: [
      { 
        cover: '/static/icon-images/直播.png',
        title: '播台之星瘦瘦',
        tag: '时尚杂志出版社',
        timer: '00:58'
      }
    ]
  },

  // 导航切换（带滚动定位）
  switchTab(e) {
    const index = e.currentTarget.dataset.index;
    const query = wx.createSelectorQuery();
    query.select(`#tab_${index}`).boundingClientRect();
    query.select('.nav-scroll').boundingClientRect();
    query.exec(res => {
      const tabRect = res[0];
      const scrollRect = res[1];
      const scrollLeft = tabRect.left - scrollRect.left - (scrollRect.width - tabRect.width)/2;
      this.setData({ 
        currentTab: index,
        scrollLeft: scrollLeft
      });
    });
  },

  // 模拟倒计时
  onLoad() {
    setInterval(() => {
      const newList = this.data.liveList.map(item => ({
        ...item,
        timer: this.formatTime(Math.max(0, parseInt(item.timer.split(':')[1]) - 1))
      }));
      this.setData({ liveList: newList });
    }, 1000);
  },

  formatTime(seconds) {
    const m = Math.floor(seconds / 60).toString().padStart(2, '0');
    const s = (seconds % 60).toString().padStart(2, '0');
    return `${m}:${s}`;
  }
});
